home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / ccdsrc.zip / PACOUT.C < prev    next >
Text File  |  1991-09-01  |  3KB  |  94 lines

  1. /* From G0/K8KA */
  2.  
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. int one_val(char *, int);
  8. /* Removes repeated DASH packets from UOSAT-5 CCD images */
  9. /* This happens on early images where the DASH resyncs and accepts
  10.    duplicate packets. Run this program on the data after you have
  11.      removed the PACSAT File Header from the file, but before any
  12.      other processing.
  13. */
  14. void main(int argc, char * argv[]){
  15.     char * thing, * oldthing;
  16.   int length=254;
  17.     int got;
  18.     int offset=0;
  19.     int skip=0;
  20.     FILE * fpin, *fpout, *trash;
  21.  
  22.     if (argc < 3){
  23.         printf("usage: pac-out <input> <output> [<packet length> [<skip>]]\n");
  24.         printf("Removes duplicate packets from early UO-5 CCD images.\n");
  25.         printf("You should not need any arguments other than <input> and <output>,\n");
  26.         printf("the two file names.\n");
  27.         printf("G0/K8KA\n");
  28.     }
  29.     if (argc > 3)
  30.         length=atoi(argv[3]);
  31.     if (argc > 4)
  32.         skip=atoi(argv[4]);
  33.  
  34.     printf("Checking for duplicate packets of length %d\n", length);
  35.     thing = (char *) malloc(length);
  36.     oldthing = (char *) calloc(length, 1);
  37.     
  38.     if (thing){
  39.         fpin = fopen(argv[1], "rb");
  40.         if (skip){
  41.             printf("Skipping %d.\n", skip);
  42.             fseek(fpin, skip, SEEK_SET);
  43.         }
  44.         else
  45.             printf("Starting from beginning of file.\n");
  46.  
  47.         if (fpin){
  48.             fpout=fopen(argv[2], "wb");
  49.             if (fpout){
  50.                 while (!feof(fpin)){
  51.                     /* Read one frame */
  52.                     got = fread(thing, 1, length, fpin);
  53.                     /*printf("Read %d : ", got);*/
  54.                     if (got){
  55.                         if (memcmp(thing, oldthing, got))
  56.                             /* Not a duplicate of the previous frame */
  57.                             fwrite(thing, 1, got, fpout);
  58.                         else if ( one_val(thing, got) ){
  59.                             /* Is a duplicate, but contains all the same pixel value */
  60.                             /* So we keep it.                                        */
  61.                             fwrite(thing, 1, got, fpout);
  62.                             printf("Duplicate kept at %d.\n", offset);
  63.                         }
  64.                         else{
  65.                             /* Is a duplicate with differing pixels, so we throw it out. */
  66.                             printf("Duplicate rejected at %d.\n", offset);
  67.                         }
  68.                     }
  69.                     memcpy(oldthing, thing, got);
  70.                     /*printf("Packet: %d\r", offset);*/
  71.                     ++offset;
  72.                 }
  73.                 fclose(fpout);
  74.             }
  75.             fclose(fpin);
  76.         }
  77.       free(thing);
  78.         free(oldthing);
  79.     }
  80. }
  81.  
  82. /* There may be some legitimate duplicate packets in the file. They
  83.    will have all one value, e.g. all 0s. This function detects them
  84.      so that we can put them into the output file.
  85. */
  86. int one_val(char *buf, int length){
  87.     int i;
  88.     for(i=1; i<length; i++){
  89.         if (buf[i]!=buf[0])
  90.             return 0;
  91.     }
  92.     return 1;
  93. }
  94.